//
//  YFNewFeatureView.swift
//  YFSwiftWeibo
//
//  Created by tsaievan on 2017/9/5.
//  Copyright © 2017年 tsaievan. All rights reserved.
//

import UIKit

class YFNewFeatureView: UIView {
    
    
    /// 欢迎界面的scrollView
    @IBOutlet weak var scrollView: UIScrollView!
    
    /// 进入微博按钮
    @IBOutlet weak var enterButton: UIButton!
    
    /// 翻页指示器
    @IBOutlet weak var pageControl: UIPageControl!
    
    /// 进入微博
    @IBAction func enterStatus(_ sender: UIButton) {
        ///< 进入微博后也将整个view移除掉
        removeFromSuperview()
    }
    
    class func newFeatureView()->YFNewFeatureView {
        let nib = UINib.init(nibName: "YFNewFeatureView", bundle: nil)
        
        ///< xib一定要关联对应的类, 下面强转才可以, 否则一强转就崩
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! YFNewFeatureView
        v.frame = UIScreen.main.bounds
        return v
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setupNewFeatureView()
    }
}


extension YFNewFeatureView {
    func setupNewFeatureView() {
        let count = 4
        let y = 0
        let width = UIScreen.hm_screenWidth()
        let height = UIScreen.hm_screenHeight()
        scrollView.contentSize = CGSize(width: width * CGFloat(count + 1), height: height)
        scrollView.isPagingEnabled = true
        scrollView.bounces = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        
        ///< 设置scrollView的代理
        scrollView.delegate = self
        
        for i in 0..<count {
            let name = "new_feature_\(i + 1)"
            let imageView = UIImageView(image: UIImage(named: name))
            imageView.frame = CGRect(x: CGFloat(i) * width, y: CGFloat(y), width: width, height: height)
            
            /**
             * 这里需要注意的一点是, 因为使用了xib
             * 在xib中, 如果添加子控件的顺序不对, 那么会造成button和pageControl被imageView挡住看不见的情况
             * 即时, 在下面的代码中使用inserView belowView 的方法也无济于事
             */
            scrollView.addSubview(imageView)
        }
        
        ///< 这里先设置进入微博的button隐藏
        enterButton.isHidden = true
    }
}

// MARK: - UIScrollViewDelegate 代理回调
extension YFNewFeatureView: UIScrollViewDelegate {
    
    ///< scrollView停止减速
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width + 0.5)
        pageControl.currentPage = page
        
        ///< 如果滚到最后一张, 就remove掉整个view
        if page == scrollView.subviews.count  {
            removeFromSuperview()
        }
        ///< enterButton 只在最后一张显示
        enterButton.isHidden = !(page == scrollView.subviews.count - 1)
    }
    
    ///< scrollView开始滚动
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        ///< 只要一滚动, enterButton就隐藏
        enterButton.isHidden = true
        ///< 如果偏移量超过4页, 则pageControl隐藏
        pageControl.isHidden = (scrollView.contentOffset.x > CGFloat(scrollView.subviews.count - 1) * (UIScreen.hm_screenWidth()))
    }
}
